package org.uugu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.uugu.dao.NewsDAO;
import org.uugu.model.News;
import org.uugu.service.AllService;
import org.uugu.service.NewsManager;
import org.uugu.util.PageControl;


@Component("newsManager")
public class NewsManagerImpl extends AllService implements NewsManager {

	// 自动注入userDao，也可以使用@Resource
	
	private NewsDAO newsDao;
	private Log log = LogFactory.getLog(NewsManagerImpl.class);
	
//=================================新闻添加====================================// 
	@Override
	public String add(News news) {
		try{
			newsDao.save(news);
			log.debug("add New:" + news.getNewsName());
			return "addSuccess";
		}catch(Exception e){
			return "error";
		}
	}
	
//=================================新闻更新====================================// 
	@Override
	public String update(News news) {
		try{
			//log.info("==========="+news.getNewsName()+news.getNewsText()+news.getNewsType()+news.getNewsHot()+news.getNewsId()+news.getNewsItem()+news.getNewsDate());
			newsDao.merge(news);
			log.debug("update New:" + news.getNewsName());
			return "updateSuccess";
		}catch(Exception e){
			return "error";
		}
	}
	
//===============================根据ID查询某条新闻===============================//
	/* (non-Javadoc)
	 * @see com.chzker.service.impl.NewsManager#listById(java.lang.Integer)
	 */
	@Override
	public List listById(Integer newsId){
		try {
			News anew2 = new News();
			anew2 = newsDao.findById(newsId);
			anew2.setNewsHot(anew2.getNewsHot()+1);
			newsDao.merge(anew2);
			List anew=newsDao.getHibernateTemplate().find("from News as n where n.newsId ="+newsId);
			//log.info("==========="+anew.size());
			if(anew.size()==1){
				return anew;
			}else{
				return null;
			}			
		} catch (Exception e) {
			return null;
		}		
	}
	

	
/* 
 * *****************************返回查询数据**********************************
 */
	@Override
	public List listAllSearchNews(String key) {
		try {
			List news = newsDao.getHibernateTemplate().find("from News as n where n.newsName like '%"+key+"%' order by n.newsId desc");
			log.info(news.size());
			return news;
		} catch (Exception e) {
			return null;
		}
	} 
	
/* 
 * ***********************分页控制核心函数************************************
 * Coder:chenzhiguo
 */
	@Override
	public List listNewsInPage(Integer item,PageControl pc) {
		try {
			int total=newsDao.findByNewsItem(item).size();
			//PageControl pc = getPageControl();
			//log.info("************"+total+item+page+pageSize);
			pc.execute(total, pc.getPageSize(), pc.getPage());	
			//log.info("$$$$$$$$$$$$$$$$$"+pc.getPage()+pc.getStartPage()+pc.getPrePage()+pc.getNextPage()+pc.getEndPage()+pc.getSub1()+pc.getSub2());
			if(total>pc.getPageSize()){
				List news = newsDao.getHibernateTemplate().find("from News as n where n.newsItem="+item+" order by n.newsId desc").subList(pc.getSub1(), pc.getSub2());
				return news;
			}else{
				List news = newsDao.getHibernateTemplate().find("from News as n where n.newsItem="+item+" order by n.newsId desc");
				return news;
			} 
			//this.setPageControl(pc);
		/*	this.getPageControl().setPage(page);
			this.getPageControl().setPageSize(pageSize);
			this.getPageControl().setPrePage(pc.getPrePage());
			this.getPageControl().setNextPage(pc.getNextPage());
			this.getPageControl().setEndPage(pc.getEndPage());*/
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
/* 
 * **********************依据信息类型和信息条数返回信息List**********************
 */	
	@Override
	public List listNewsByNumAndItem(int i,int item) {
		try {
				if(i<=newsDao.findByNewsItem(item).size()){
					List news=newsDao.getHibernateTemplate().find("from News as n where n.newsItem="+item+" order by n.newsId desc").subList(0, i);
					return news;
				}else{
					List news=newsDao.getHibernateTemplate().find("from News as n where n.newsItem="+item+" order by n.newsId desc");
					return news;
				}
		} catch (Exception e) {
			return null;
		}
	}
	
/******************************根据ID删除信息**********************************/
	@Override
	public String delById(Integer newsId) {
		try{
			newsDao.delete(newsDao.findById(newsId));
			return "delSuccess";
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
	}

//===============================Getters And Setters==========================//

	@Override
	public NewsDAO getNewsDao() {
		return newsDao;
	}
	@Override
	@Resource
	public void setNewsDao(NewsDAO newsDao) {
		this.newsDao = newsDao;
	}


}
